package com.easyim.imserver.config;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;

@Configuration
@Slf4j
public class RedisConfig {

    @Autowired
    RedisConfigProperties redisConfigProperties;


    @Bean("msgRedisTemplate")
    public RedisTemplate msgRedisTemplate(){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(msgRedisRepo());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // Set the value serializer (Jackson2JsonRedisSerializer for JSON)
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        // Set the hash key serializer
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // Set the hash value serializer
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        return redisTemplate;
    }

    private RedisConnectionFactory msgRedisRepo() {
        RedisConnectionProperties msgConfig = redisConfigProperties.getMsg();
        log.info("start init msg redis client, config:{}", JSONObject.toJSONString(msgConfig));
        if("standalone".equals(msgConfig.getType())){
             RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
             redisConfiguration.setHostName(redisConfiguration.getHostName());
             redisConfiguration.setPort(redisConfiguration.getPort());
             if(StringUtils.hasText(msgConfig.getUsername())){
                 redisConfiguration.setUsername(msgConfig.getUsername());
             }
             if(StringUtils.hasText(msgConfig.getPassword())){
                redisConfiguration.setPassword(msgConfig.getPassword());
             }
             LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisConfiguration);
             lettuceConnectionFactory.afterPropertiesSet();
             return lettuceConnectionFactory;
         }else if("cluster".equals(msgConfig.getType())){
            RedisClusterConfiguration redisConfiguration = new RedisClusterConfiguration();
            String[] hosts = msgConfig.getHostName().split(",");
            List<RedisNode> nodes = new LinkedList<>();
            for (int i = 0; i < hosts.length; i++) {
                String[] ipPort = hosts[i].split(":");
                RedisNode redisNode = new RedisNode(ipPort[0], Integer.parseInt(ipPort[1]));
                nodes.add(redisNode);
            }
            redisConfiguration.setClusterNodes(nodes);
            if(StringUtils.hasText(msgConfig.getPassword())){
                redisConfiguration.setPassword(msgConfig.getPassword());
            }
            LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisConfiguration);
            lettuceConnectionFactory.afterPropertiesSet();
            return lettuceConnectionFactory;
         }
         //哨兵一般已经见不到了 就不做了 主要把im做好
         return null;
    }

}
